package com.zrh.jsd.service.studydemo.daemon_thread;

import org.openjdk.jol.info.ClassLayout;

public class ThreadStatePrint {
    public static void main(String[] args) throws InterruptedException {
        User userTemp = new User();
        //  0x0000000000000001 (non-biasable; age: 0)  001 代表【无锁】状态
        System.out.println("无状态(001)-- "+ClassLayout.parseInstance(userTemp).toPrintable());
        /*
        * jvm默认延时4s开启偏向锁，可以通过设置取消延时，也可以通过设置关闭偏向锁
        * */
        Thread.sleep(10000);
        User user = new User();
        // 0x0000000000000005 (biasable; age: 0)  可偏向状态，虽然后三位101，没有具体线程，线程Id=0;
        System.out.println("启用偏向锁(101)-- "+ClassLayout.parseInstance(user).toPrintable());
        for (int i = 0; i < 2; i++) {
            synchronized (user){
                // 0x00000261d399e005 (biased: 0x000000009874e678; epoch: 0; age: 0) 已偏向状态，后三位101，有具体偏向线程ID
                System.out.println("偏向锁101(带线程id)：" + ClassLayout.parseInstance(user).toPrintable());
            }
            System.out.println("偏向锁释放101(带线程id) " + ClassLayout.parseInstance(user).toPrintable());
        }
        new Thread(()->{
            synchronized (user){
                System.out.println("轻量级锁00 " + ClassLayout.parseInstance(user).toPrintable());
            }
        }).start();
    }
}
